﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Mechanics.GameEventMechanics {
    import Mechanics.GameMechanicInterfaces.*;
    import Mechanics.*;
    import Classes.*;
    import Classes.util.*;
    import Modules.crew.*;
    import Mechanics.Transactions.*;

    public class RollCallDataMechanic implements IActionGameMechanic {

        public static const STATE_VIRGIN:String = "virgin";
        public static const STATE_IN_PROGRESS:String = "in_progress";
        public static const STATE_COMPLETE:String = "complete";
        public static const STATE_FINISHED:String = "finished";

        private const MECHANIC_TYPE:String = "rollCall";
        private const TIMESTAMP_PREFIX:String = "rollCall_";
        private const SECONDS_PER_HOUR:Number = 3600;
        private const MILLIS_PER_HOUR:Number = 3600000;

        private var m_owner:MechanicMapResource;
        private var m_config:MechanicConfigData;
        private var m_state:String;

        public function hasOverrideForGameAction(_arg1:String):Boolean{
            return ((this[_arg1] is Function));
        }
        public function get owner():MechanicMapResource{
            return (this.m_owner);
        }
        public function get requiredLevel():int{
            return (int(this.m_config.params["requiredLevel"]));
        }
        public function executeOverrideForGameEvent(_arg1:String, _arg2:Array=null):MechanicActionResult{
            var _local3:MechanicActionResult = new MechanicActionResult(false, true);
            if ((this[_arg1] is Function)){
                _local3 = this[_arg1].apply(this, _arg2);
            };
            return (_local3);
        }
        public function initialize(_arg1:IMechanicUser, _arg2:MechanicConfigData):void{
            this.m_owner = (_arg1 as MechanicMapResource);
            this.m_config = _arg2;
        }
        public function isVisitEnabled():Boolean{
            return (false);
        }
        public function setFinished():Boolean{
            var _local1:Object = this.getData();
            if (_local1["state"] == STATE_COMPLETE){
                _local1["state"] = STATE_FINISHED;
                this.sendTransaction("finishEverything");
                return (true);
            };
            return (false);
        }
        public function checkIn(_arg1:String, _arg2:Boolean=false):Boolean{
            var _local4:Boolean;
            var _local5:int;
            var _local3:Object = this.getData();
            if (this.canCheckInHelper(_arg1, _local3)){
                if (_arg2 == true){
                    _local5 = Global.gameSettings.getInt("rollCallSkipCheckinPrice", 30);
                    if (Global.player.cash < _local5){
                        return (false);
                    };
                    Global.player.cash = (Global.player.cash - _local5);
                };
                _local4 = this.addCheckinData(_local3, _arg1, _arg2);
                if (_local4 == true){
                    this.sendTransaction("checkIn", _arg1, _arg2);
                };
                return (_local4);
            };
            return (false);
        }
        public function collect(_arg1:String, _arg2:Boolean=true):Boolean{
            var _local4:Boolean;
            var _local3:Object = this.getData();
            if (this.canCollectHelper(_arg1, _local3)){
                _local4 = this.addCollectData(_local3, _arg1);
                if ((((_local4 == true)) && (_arg2))){
                    this.sendTransaction("collect", _arg1);
                };
                return (_local4);
            };
            return (false);
        }
        public function feedSent(_arg1:String):Boolean{
            var _local3:Boolean;
            var _local2:Object = this.getData();
            if (this.canSendFeed(_arg1, _local2)){
                _local3 = this.addFeedSendData(_local2, _arg1);
                if (_local3 == true){
                    this.sendTransaction("feedSent", _arg1);
                };
                return (_local3);
            };
            return (false);
        }
        public function startRollCall():Boolean{
            var _local1:Object = this.getData();
            if (this.canPerformRollCall()){
                _local1 = this.initData();
                _local1["state"] = STATE_IN_PROGRESS;
                _local1["stateChangeTime"] = uint((GlobalEngine.getTimer() / 1000));
                this.m_owner.setDataForMechanic(this.MECHANIC_TYPE, _local1);
                this.sendTransaction("startRollCall");
                this.setGlobalRollCallCooldownTimestamp();
                return (true);
            };
            return (false);
        }
        public function canCollect(_arg1:String):Boolean{
            var _local2:Object = this.getData();
            return (this.canCollectHelper(_arg1, _local2));
        }
        public function getRollCallTimeLeft():Number{
            var _local1:Object = this.getData();
            return (this.getRollCallTimeLeftHelper(_local1));
        }
        public function getTimeLeftToCollect():Number{
            var _local1:Object = this.getData();
            return (this.getTimeLeftToCollectHelper(_local1));
        }
        public function isRollCallComplete():Boolean{
            var _local1:Object = this.getData();
            if (_local1["state"] == STATE_COMPLETE){
                return (true);
            };
            return ((((_local1["state"] == STATE_IN_PROGRESS)) && ((this.getRollCallTimeLeftHelper(_local1) <= 0))));
        }
        public function getState():String{
            var _local1:Object = this.getData();
            return (_local1["state"]);
        }
        public function canPerformRollCall():Boolean{
            var _local1:Object = this.getData();
            var _local2:Boolean = (((_local1["state"] == STATE_VIRGIN)) || (this.isCooldownComplete(_local1)));
            var _local3:Boolean = ((Global.isVisiting()) || ((Global.player.level >= this.requiredLevel)));
            var _local4:Boolean = this.isGlobalCooldownComplete();
            return (((((_local2) && (_local3))) && (_local4)));
        }
        public function getCrewList():Array{
            var _local3:String;
            var _local1:Array = new Array();
            var _local2:Crew = Global.crews.getCrewById(this.m_owner.getId());
            if (_local2 != null){
                for each (_local3 in _local2.list) {
                    _local1.push(_local3);
                };
            };
            return (_local1);
        }
        public function getCrewState():Array{
            var _local6:String;
            var _local7:String;
            var _local8:Object;
            var _local1:Object = {};
            var _local2:Crew = Global.crews.getCrewById(this.m_owner.getId());
            var _local3:Object = this.getData();
            var _local4:Array = new Array();
            var _local5 = 1;
            if (_local2 != null){
                for each (_local6 in _local2.list) {
                    _local7 = ("i" + _local6);
                    _local8 = new Object();
                    if (_local3[_local7]){
                        _local8["position"] = this.m_owner.getCrewPositionName(_local6);
                        _local8["zid"] = _local6;
                        _local8["checkedIn"] = _local3[_local7]["checkedIn"];
                        _local8["collected"] = _local3[_local7]["collected"];
                        _local8["purchasedCheckIn"] = _local3[_local7]["purchasedCheckIn"];
                        _local8["count"] = _local3[_local7]["count"];
                        _local8["feedSent"] = _local3[_local7]["feedSent"];
                        _local8["orderId"] = _local5;
                        _local5++;
                        _local4.push(_local8);
                    };
                };
            };
            return (_local4);
        }
        public function canCheckIn(_arg1:String):Boolean{
            var _local2:Object = this.getData();
            return (this.canCheckInHelper(_arg1, _local2));
        }
        public function hasCollected(_arg1:String):Boolean{
            var _local2:Object = this.getData();
            return (this.hasCollectedHelper(_arg1, _local2));
        }
        private function isGlobalCooldownComplete():Boolean{
            var _local1:String = this.m_config.params["category"];
            if (Global.isVisiting() == true){
                return (true);
            };
            var _local2:int = Global.player.getLastActivationTime((this.TIMESTAMP_PREFIX + _local1));
            if (_local2 < 0){
                return (true);
            };
            var _local3:Number = (Number(this.m_config.params["cooldown"]) * this.SECONDS_PER_HOUR);
            var _local4:Number = uint((GlobalEngine.getTimer() / 1000));
            return ((_local4 > (_local2 + _local3)));
        }
        private function setGlobalRollCallCooldownTimestamp():void{
            var _local1:String = this.m_config.params["category"];
            var _local2:Number = uint((GlobalEngine.getTimer() / 1000));
            Global.player.setLastActivationTime((this.TIMESTAMP_PREFIX + _local1), _local2);
        }
        private function isCitySamZid(_arg1:String):Boolean{
            return ((_arg1.charAt(0) == "-"));
        }
        private function getRollCallTimeLeftHelper(_arg1:Object):Number{
            if (_arg1["state"] != STATE_IN_PROGRESS){
                return (-1);
            };
            var _local2:Number = (_arg1["stateChangeTime"] as Number);
            var _local3:Number = (Number(this.m_config.params["rollCallTimeLimit"]) * this.SECONDS_PER_HOUR);
            var _local4:Number = uint((GlobalEngine.getTimer() / 1000));
            return (((_local2 + _local3) - _local4));
        }
        private function getTimeLeftToCollectHelper(_arg1:Object):Number{
            if (_arg1["state"] != STATE_COMPLETE){
                return (-1);
            };
            var _local2:Number = (_arg1["stateChangeTime"] as Number);
            var _local3:Number = (Number(this.m_config.params["collectionTimeLimit"]) * this.SECONDS_PER_HOUR);
            var _local4:Number = uint((GlobalEngine.getTimer() / 1000));
            return (((_local2 + _local3) - _local4));
        }
        private function getData():Object{
            var _local1:Object;
            var _local2:Number;
            _local1 = this.m_owner.getDataForMechanic(this.MECHANIC_TYPE);
            if (((!(_local1)) || ((_local1 == MechanicManager.DATA_NOT_INITIALIZED)))){
                _local1 = this.initData();
            };
            switch (_local1["state"]){
                case STATE_VIRGIN:
                    break;
                case STATE_IN_PROGRESS:
                    _local2 = this.getRollCallTimeLeftHelper(_local1);
                    if ((((_local2 <= 0)) || (this.areCheckinsComplete(_local1)))){
                        _local1["state"] = STATE_COMPLETE;
                        _local1["stateChangeTime"] = uint((GlobalEngine.getTimer() / 1000));
                        this.m_owner.setDataForMechanic(this.MECHANIC_TYPE, _local1);
                    };
                    break;
                case STATE_COMPLETE:
                    break;
                case STATE_FINISHED:
                    break;
            };
            _local1 = this.addMissingCrew(_local1);
            return (_local1);
        }
        private function addMissingCrew(_arg1:Object):Object{
            var _local3:String;
            var _local2:Crew = Global.crews.getCrewById(this.m_owner.getId());
            if (_local2 != null){
                for each (_local3 in _local2.list) {
                    _local3 = ("i" + _local3);
                    if (!_arg1[_local3]){
                        _arg1[_local3] = this.initCrewEntry();
                    };
                };
            };
            return (_arg1);
        }
        private function areCheckinsComplete(_arg1:Object):Boolean{
            var _local3:String;
            var _local2:Crew = Global.crews.getCrewById(this.m_owner.getId());
            if (_local2 == null){
                return (false);
            };
            for each (_local3 in _local2.list) {
                _local3 = ("i" + _local3);
                if (((((!(_arg1[_local3])) || (!(_arg1[_local3]["checkedIn"])))) || ((_arg1[_local3]["checkedIn"] == false)))){
                    return (false);
                };
            };
            return (true);
        }
        private function addCheckinData(_arg1:Object, _arg2:String, _arg3:Boolean):Boolean{
            if (((this.isCrewMember(_arg2)) && ((_arg1["state"] == STATE_IN_PROGRESS)))){
                _arg2 = ("i" + _arg2);
                if (!_arg1[_arg2]){
                    _arg1[_arg2] = this.initCrewEntry();
                };
                if (_arg1[_arg2]["checkedIn"] == false){
                    _arg1[_arg2]["checkedIn"] = true;
                    _arg1[_arg2]["purchasedCheckIn"] = _arg3;
                    var _local4 = _arg1[_arg2];
                    var _local5 = "count";
                    var _local6 = (_local4[_local5] + 1);
                    _local4[_local5] = _local6;
                };
                this.m_owner.setDataForMechanic(this.MECHANIC_TYPE, _arg1);
                return (true);
            };
            return (false);
        }
        private function addFeedSendData(_arg1:Object, _arg2:String):Boolean{
            if (((this.isCrewMember(_arg2)) && ((_arg1["state"] == STATE_IN_PROGRESS)))){
                _arg2 = ("i" + _arg2);
                if (!_arg1[_arg2]){
                    _arg1[_arg2] = this.initCrewEntry();
                };
                if (_arg1[_arg2]["feedSent"] == false){
                    _arg1[_arg2]["feedSent"] = true;
                };
                this.m_owner.setDataForMechanic(this.MECHANIC_TYPE, _arg1);
                return (true);
            };
            return (false);
        }
        private function addCollectData(_arg1:Object, _arg2:String):Boolean{
            if (((((((this.isCrewMember(_arg2)) || ((Global.isVisiting() == false)))) && ((_arg1["state"] == STATE_COMPLETE)))) && ((this.getTimeLeftToCollectHelper(_arg1) > 0)))){
                _arg2 = ("i" + _arg2);
                if (!_arg1[_arg2]){
                    _arg1[_arg2] = this.initCrewEntry();
                };
                if (_arg1[_arg2]["collected"] == false){
                    _arg1[_arg2]["collected"] = true;
                };
                this.m_owner.setDataForMechanic(this.MECHANIC_TYPE, _arg1);
                return (true);
            };
            return (false);
        }
        public function hasCheckedIn(_arg1:String):Boolean{
            var _local2:Object = this.getData();
            return (this.hasCheckedInHelper(_arg1, _local2));
        }
        private function initData():Object{
            var _local3:String;
            var _local1:Object = {};
            _local1["state"] = STATE_VIRGIN;
            _local1["stateChangeTime"] = 0;
            var _local2:Crew = Global.crews.getCrewById(this.m_owner.getId());
            if (_local2 != null){
                for each (_local3 in _local2.list) {
                    _local1[("i" + _local3)] = this.initCrewEntry();
                };
            };
            this.m_owner.setDataForMechanic(this.MECHANIC_TYPE, _local1);
            return (_local1);
        }
        private function isCooldownComplete(_arg1:Object):Boolean{
            var _local2:Number;
            var _local3:Number;
            var _local4:Number;
            if (_arg1["state"] == STATE_COMPLETE){
                _local2 = _arg1["stateChangeTime"];
                _local3 = (Number(this.m_config.params["cooldown"]) * this.SECONDS_PER_HOUR);
                _local4 = uint((GlobalEngine.getTimer() / 1000));
                return ((_local4 > (_local2 + _local3)));
            };
            return (false);
        }
        private function canSendFeed(_arg1:String, _arg2:Object):Boolean{
            return ((((((((_arg2["state"] == STATE_IN_PROGRESS)) && (this.isCrewMember(_arg1)))) && ((this.getRollCallTimeLeftHelper(_arg2) > 0)))) && (((!(_arg2[("i" + _arg1)])) || ((_arg2[("i" + _arg1)]["feedSent"] == false))))));
        }
        private function canCollectHelper(_arg1:String, _arg2:Object):Boolean{
            return ((((((((_arg2["state"] == STATE_COMPLETE)) && ((this.getTimeLeftToCollectHelper(_arg2) > 0)))) && (!(this.hasCollectedHelper(_arg1, _arg2))))) && ((((Global.isVisiting() == false)) || (((((this.isCrewMember(_arg1)) && (this.hasCheckedInHelper(_arg1, _arg2)))) && (!(this.hasPurchasedCheckIn(_arg1, _arg2)))))))));
        }
        private function hasPurchasedCheckIn(_arg1:String, _arg2:Object):Boolean{
            _arg1 = ("i" + _arg1);
            if (((((_arg2) && (_arg2[_arg1]))) && (_arg2[_arg1]["purchasedCheckIn"]))){
                return (_arg2[_arg1]["purchasedCheckIn"]);
            };
            return (false);
        }
        private function canCheckInHelper(_arg1:String, _arg2:Object):Boolean{
            return ((((((((_arg2["state"] == STATE_IN_PROGRESS)) && (this.isCrewMember(_arg1)))) && ((this.getRollCallTimeLeftHelper(_arg2) > 0)))) && (!(_arg2[("i" + _arg1)]["checkedIn"]))));
        }
        private function hasCollectedHelper(_arg1:String, _arg2:Object):Boolean{
            _arg1 = ("i" + _arg1);
            if (((_arg2[_arg1]) && (_arg2[_arg1]["collected"]))){
                return (_arg2[_arg1]["collected"]);
            };
            return (false);
        }
        private function hasCheckedInHelper(_arg1:String, _arg2:Object):Boolean{
            _arg1 = ("i" + _arg1);
            if (((_arg2[_arg1]) && (_arg2[_arg1]["checkedIn"]))){
                return (_arg2[_arg1]["checkedIn"]);
            };
            return (false);
        }
        private function isCrewMember(_arg1:String):Boolean{
            var _local3:String;
            var _local2:Crew = Global.crews.getCrewById(this.m_owner.getId());
            if (_local2 != null){
                for each (_local3 in _local2.list) {
                    if (_local3 === _arg1){
                        return (true);
                    };
                };
            };
            return (false);
        }
        public function isActiveObject():Boolean{
            var _local1:String = this.m_config.params["category"];
            var _local2:MechanicMapResource = Global.rollCallManager.getActiveObject(_local1);
            return (((_local2) && ((_local2.getId() == this.m_owner.getId()))));
        }
        private function initCrewEntry():Object{
            var _local1:Object = {};
            _local1["collected"] = false;
            _local1["count"] = 0;
            _local1["checkedIn"] = false;
            _local1["purchasedCheckIn"] = false;
            _local1["feedSent"] = false;
            return (_local1);
        }
        private function sendTransaction(_arg1:String, ... _args):void{
            GameTransactionManager.addTransaction(new TMechanicAction(this.m_owner, MechanicManager.MECHANIC_ROLL_CALL, MechanicManager.ALL, {
                operation:_arg1,
                args:_args
            }), true);
        }

    }
}//package Mechanics.GameEventMechanics 
